package group9;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * @author Claudio Herreros, Dylan James, Tyler Berg
 *
 */

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class UserStory implements Serializable{

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	
	@Persistent(defaultFetchGroup="true")
	private User creator;

	@Persistent
	private String title;

	@Persistent
	private String description;

	@Persistent
	private String testNotes;
	
	@Persistent
	private String date;

	// cost
	@Persistent(mappedBy="userStory")
	private ArrayList<Estimate> estimates;
	
	@Persistent
	private int estimatesRemaining;

	@Persistent
	private int worth;
	
	@Persistent
	private ArrayList<Key> estimators;

	public UserStory(String title, String description, String testNotes, User creator) {
		this.creator = creator;
		estimates = new ArrayList<Estimate>();
		this.estimatesRemaining = 0;
		worth = 0;
		this.testNotes = testNotes;
		date = null;
		this.title = title;
		this.description = description;
	}

	public Key getKey()
	{
		return this.key;
	}
	public String getTitle()
	{
		return this.title;
	}
	
	public String getDescription()
	{
		return this.description;
	}
	
	public String getTestNotes()
	{
		return this.testNotes;
	}
	
	public void setTitle(String title){
		this.title = title;
	}
	
	public void setDescription(String description){
		this.description = description;
	}
	
	public void setTestNotes(String testNotes){
		this.testNotes = testNotes;
	}
	
	public void setEstimates(ArrayList<Estimate> estims){
		estimates = estims;
	}
	
	public int getEstimate()
	{
		return worth;
	}
	
	public ArrayList<Estimate> getEstimates()
	{
		return estimates;
	}
	// This method is not working correctly, gets a null pointer exception.
	public void addEstimator(User user)
	{
		//user.addEstUserStory(this.key);
		this.estimates.add(new Estimate(null, this, user));
		this.estimatesRemaining++;
		//this.estimators.add(user.getKey());
	}
	
	public boolean existsEstimator(User user)
	{
		for(int i = 0; i < estimates.size(); i++){
			if(estimates.get(i).getUserKey().equals(user.getKey())){
				return true;
			}
		}
		return false;
	}
	
	public boolean estimate(User user, Integer estimate)
	{
		return estimate(user.getKey(), estimate);
	}
	
	public boolean estimate(Key key, Integer estimate)
	{
		int index = -1;
		
		for (int i = 0; i < estimates.size(); i++)
		{
			if (estimates.get(i).getUserKey() == key)
			{
				index = i;
				break;
			}
		}
		
		if (index == -1)
			return false;
		
		Estimate est = estimates.get(index);
		est.setValue(estimate);
		
		estimates.set(index, est);
		this.estimatesRemaining--;
		
		if (!pending())
		{
			int ests = 0;
			int total = 0;
			for (Estimate es : estimates)
			{
				total += es.getValue();
				ests++;
			}
			
			this.worth = total / ests;
		}
		return true;
	}
	
	public boolean pending()
	{
		return this.estimatesRemaining != 0 || estimates.isEmpty();
	}

	public User getCreator() {
		return creator;
	}

	public ArrayList<Key> getEstimators() {
		return estimators;
	}
}
